% Compute first part of Table 2 in Ritschl, Sarferaz, and Uebele (2015)
clear all
clc

addpath('funcs');
addpath('scrpts');
%% Prepare data and results
% See run_table1.m

% Define different periods
tinterval.ft_preWW1 = 1:47;
tinterval.ft_14to29 = 48:63;
tinterval.ft_30to39 = 64:73;
tinterval.ft_postWW2 = 80:140;
tinterval.ft_preWW2 = 1:79;
period = {'preWW1','14to29','30to39','preWW2','postWW2'};
save('matFiles/tinterval','period','tinterval');
%% Load results and compute standard deviations for Table 1
clear all; clc;
% Define subsets
subset_load= {'all'};
tv_spec = {'intermediate';'tight'};
tv_spec2 = {'intermediate';'tight_71_01'};
save('matFiles/subset_load','subset_load');
save('matFiles/tv_spec','tv_spec','tv_spec2');

for px=1:size(tv_spec,1)
    save('matFiles/px','px');
    load('matFiles/subset_load');
    
    for ix =1:size(subset_load,1)
        load('matFiles/px');
        load('matFiles/tv_spec');
        load('matFiles/tinterval');
        load('matFiles/subset_load');
        load('data/data_gdp');
        filename_sub = char(['matFiles/' tv_spec{px} '/results_52_1867_2006_',subset_load{ix},'_',tv_spec2{px}]);
        load(filename_sub);
        
        
        disp([subset_load{ix},'_',tv_spec{px}]);
        
        eval(['T_pre=size(results.ft(:,tinterval.ft_' period{4} ',:),2);']);
        eval(['T_post=size(results.ft(:,tinterval.ft_' period{5} ',:),2);']);
        eval(['ft_preWW2=results.ft(:,tinterval.ft_' period{4} ' ,:);']);
        eval(['ft_postWW2=results.ft(:,tinterval.ft_' period{5} ' ,:);']);
        ratio_ft = std(ft_postWW2,0,2)./std(ft_preWW2,0,2);
        
        if ix==5
            ft_stand_pre = ft_preWW2*std_cpi./(repmat(ratio_ft,[1,T_pre,1]).*repmat(std(ft_preWW2,0,2), [1,T_pre,1]));
            ft_stand_post = ft_postWW2*std_cpi./repmat(std(ft_postWW2,0,2), [1,size(ft_postWW2,2),1]);
        else
            ft_stand_pre = ft_preWW2*std_nipa./(repmat(ratio_ft,[1,T_pre,1]).*repmat(std(ft_preWW2,0,2), [1,T_pre,1]));
            ft_stand_post = ft_postWW2*std_nipa./repmat(std(ft_postWW2,0,2), [1,size(ft_postWW2,2),1]);
        end
        ft_stand = cat(2,ft_stand_pre,ft_stand_post);
        
        %     for px=1:size(period,2)-1 % without preWW2
        %         eval(['ft_all.' subset_load{ix} '_' period{px} '=ft_stand(:,tinterval.ft_' period{px} ',:);']);
        %     end
        
        eval(['perstd.' subset_load{ix} '_' period{1} '_'  tv_spec{px} '_med' '= median(std(ft_stand(:,tinterval.ft_' period{1} ',:),0,2),3);']);
        eval(['perstd.' subset_load{ix} '_' period{1} '_'  tv_spec{px} '_std' '= std(std(ft_stand(:,tinterval.ft_' period{1} ',:),0,2),0,3);']);
        
        eval(['perstd.' subset_load{ix} '_' period{2} '_'  tv_spec{px} '_med' '= median(std(ft_stand(:,tinterval.ft_' period{2} ',:),0,2),3);']);
        eval(['perstd.' subset_load{ix} '_' period{2} '_'  tv_spec{px} '_std' '= std(std(ft_stand(:,tinterval.ft_' period{2} ',:),0,2),0,3);']);
        
        eval(['perstd.' subset_load{ix} '_' period{3} '_'  tv_spec{px} '_med' '= median(std(ft_stand(:,tinterval.ft_' period{3} ',:),0,2),3);']);
        eval(['perstd.' subset_load{ix} '_' period{3} '_'  tv_spec{px} '_std' '= std(std(ft_stand(:,tinterval.ft_' period{3} ',:),0,2),0,3);']);
        
        eval(['temp=std(ft_stand(:,tinterval.ft_' period{2} ',:),0,2)./std(ft_stand(:,tinterval.ft_' period{1} ',:),0,2);']);
        eval(['perstd.' subset_load{ix} '_'  tv_spec{px} '_14to29preWW1_med' '= median(temp,3);']);
        eval(['perstd.' subset_load{ix} '_'  tv_spec{px} '_14to29preWW1_std' '= std(temp,0,3);']);
        
        
        save('matFiles/factors_table2_I','perstd');%'ft_all',
        
        clear all;
        load('matFiles/factors_table2_I');
    end
end
%% Create Excel-file of results
load('matFiles/subset_load');
load('matFiles/factors_table2_I');

perstd_c = struct2cell(perstd);
for ix=1:size(subset_load,1)*2
    perstd_mat(ix,:) = cell2mat(perstd_c(1+8*(ix-1):8+8*(ix-1)))'.*[ones(1,6)*100 ones(1,2)];
end


names_col = {'','medPreWW1','stdPreWW1','med14to29','std14to29',...
    'med30to39','std30t39','med14to29preWW1','std14to29preWW1'};
names_row = [subset_load;subset_load];

xlswrite('tables/volaComparison_final', names_col, 'Table2_I_fromMatlab', 'A1');
xlswrite('tables/volaComparison_final', names_row, 'Table2_I_fromMatlab', 'A2');
xlswrite('tables/volaComparison_final', perstd_mat, 'Table2_I_fromMatlab', 'B2');
